public class ThreadDemo16 {
    static class Counter{
        //变量
        private int number=0;
        //循环次数
        private int MAX_COUNT=0;
        public Counter(int MAX_COUNT){
            this.MAX_COUNT=MAX_COUNT;
        }

        //++方法
        public void incr(){
            for (int i = 0; i <MAX_COUNT ; i++) {
                number++;
            }
        }

        //--方法
        public void decr(){
            for (int i = 0; i <MAX_COUNT ; i++) {
                number--;
            }
        }

        public int getNumber(){
            return number;
        }

        public static void main(String[] args) throws InterruptedException {
            Counter counter=new Counter(1000000);
            Thread t1=new Thread(()->{
                //++操作
                counter.incr();
            });
           Thread t2=new Thread(()->{
               //--操作
               counter.decr();
           });
           //启动线程
            t1.start();
            t2.start();

            //等待两个线程执行完
            t1.join();
            t2.join();
            System.out.println("最终结果:"+counter.getNumber());
        }
    }
}
